package com.mitgroup.dmrvs.view;

import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;

import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.mitgroup.dmrvs.R;
import com.mitgroup.dmrvs.utils.AppManager;
import com.mitgroup.dmrvs.utils.Utils;
import com.mitgroup.dmrvs.webservice.WSClient;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.trinea.android.common.util.FileUtils;
import cn.trinea.android.common.util.PreferencesUtils;

public class DispWorkOrderActivity extends AppCompatActivity {
    private static final int MSG_GET_WORK_ORDER= 1;
    private static final String BUNDLE_LOCAL_ERROR = "local_error";
    private static final String BUNDLE_MSG_GET_WORK_ORDER = "get_work_order_item";
    private static final String BUNDLE_MSG_GET_WORK_ORDER_SUCCESS= "get_work_order_success";
    private static final String BUNDLE_MSG_GET_WORK_ORDER_FAIL= "get_work_order_fail";
    private Intent intent;
    private ProgressDialog progressDialog;
    /**handler对象*/
    private Handler handler;
    String userName;
    String carInfoId;
    String userId;
    String workOrderContent;
    // 模板地址
    private static final String templetPath = Environment.getExternalStorageDirectory()+"/doc/workOrderTemplate.doc";
    // 创建生成的文件地址
    private static final String newPath = Environment.getExternalStorageDirectory()+"/doc/workOrder.doc";
    @BindView(R.id.btn_back)
    public Button btnBack;
    @OnClick(R.id.btn_back)
    public void onBack(View view) {
        this.finish();
    }
    @BindView(R.id.tv_user_name)
    public TextView tvUserName;
    @BindView(R.id.btn_nav01)
    public Button btnReceiveCar;
    @OnClick(R.id.btn_nav01)
    public void onReceiveCar(View view) {
        intent = new Intent(DispWorkOrderActivity.this,
                ReceiveCarActivity.class);
        startActivity(intent);
    }
    @BindView(R.id.btn_nav02)
    public Button btnDiagnose;
    @OnClick(R.id.btn_nav02)
    public void onDiagnose(View view) {
        intent = new Intent(DispWorkOrderActivity.this,
                CarDiagnoseActivity.class);
        startActivity(intent);
    }
    @BindView(R.id.btn_nav03)
    public Button btnSettings;
    @OnClick(R.id.btn_nav03)
    public void onSettings(View view) {
        intent = new Intent(DispWorkOrderActivity.this,
                SettingsActivity.class);
        startActivity(intent);
    }
    @BindView(R.id.iv_quit)
    public Button quit;
    @OnClick(R.id.iv_quit)
    public void onQuit(View view) {
        Utils.Exit(this);
    }

    @BindView(R.id.tv_item_name_1)
    public TextView tvItemName1;
    @BindView(R.id.tv_man_hour_fee_1)
    public TextView tvManHourFee1;
    @BindView(R.id.tv_spare_part_name_1)
    public TextView tvSparePartName1;
    @BindView(R.id.tv_spare_part_number_1)
    public TextView tvSparePartNumber1;
    @BindView(R.id.tv_price_1)
    public TextView tvPrice1;

    @BindView(R.id.tv_item_name_2)
    public TextView tvItemName2;
    @BindView(R.id.tv_man_hour_fee_2)
    public TextView tvManHourFee2;
    @BindView(R.id.tv_spare_part_name_2)
    public TextView tvSparePartName2;
    @BindView(R.id.tv_spare_part_number_2)
    public TextView tvSparePartNumber2;
    @BindView(R.id.tv_price_2)
    public TextView tvPrice2;

    @BindView(R.id.tv_item_name_3)
    public TextView tvItemName3;
    @BindView(R.id.tv_man_hour_fee_3)
    public TextView tvManHourFee3;
    @BindView(R.id.tv_spare_part_name_3)
    public TextView tvSparePartName3;
    @BindView(R.id.tv_spare_part_number_3)
    public TextView tvSparePartNumber3;
    @BindView(R.id.tv_price_3)
    public TextView tvPrice3;

    @BindView(R.id.tv_item_name_4)
    public TextView tvItemName4;
    @BindView(R.id.tv_man_hour_fee_4)
    public TextView tvManHourFee4;
    @BindView(R.id.tv_spare_part_name_4)
    public TextView tvSparePartName4;
    @BindView(R.id.tv_spare_part_number_4)
    public TextView tvSparePartNumber4;
    @BindView(R.id.tv_price_4)
    public TextView tvPrice4;

    @BindView(R.id.tv_item_name_5)
    public TextView tvItemName5;
    @BindView(R.id.tv_man_hour_fee_5)
    public TextView tvManHourFee5;
    @BindView(R.id.tv_spare_part_name_5)
    public TextView tvSparePartName5;
    @BindView(R.id.tv_spare_part_number_5)
    public TextView tvSparePartNumber5;
    @BindView(R.id.tv_price_5)
    public TextView tvPrice5;

    @BindView(R.id.tv_item_name_6)
    public TextView tvItemName6;
    @BindView(R.id.tv_man_hour_fee_6)
    public TextView tvManHourFee6;
    @BindView(R.id.tv_spare_part_name_6)
    public TextView tvSparePartName6;
    @BindView(R.id.tv_spare_part_number_6)
    public TextView tvSparePartNumber6;
    @BindView(R.id.tv_price_6)
    public TextView tvPrice6;

    @BindView(R.id.tv_item_name_7)
    public TextView tvItemName7;
    @BindView(R.id.tv_man_hour_fee_7)
    public TextView tvManHourFee7;
    @BindView(R.id.tv_spare_part_name_7)
    public TextView tvSparePartName7;
    @BindView(R.id.tv_spare_part_number_7)
    public TextView tvSparePartNumber7;
    @BindView(R.id.tv_price_7)
    public TextView tvPrice7;

    @BindView(R.id.tv_item_name_8)
    public TextView tvItemName8;
    @BindView(R.id.tv_man_hour_fee_8)
    public TextView tvManHourFee8;
    @BindView(R.id.tv_spare_part_name_8)
    public TextView tvSparePartName8;
    @BindView(R.id.tv_spare_part_number_8)
    public TextView tvSparePartNumber8;
    @BindView(R.id.tv_price_8)
    public TextView tvPrice8;

    @BindView(R.id.tv_item_name_9)
    public TextView tvItemName9;
    @BindView(R.id.tv_man_hour_fee_9)
    public TextView tvManHourFee9;
    @BindView(R.id.tv_spare_part_name_9)
    public TextView tvSparePartName9;
    @BindView(R.id.tv_spare_part_number_9)
    public TextView tvSparePartNumber9;
    @BindView(R.id.tv_price_9)
    public TextView tvPrice9;

    @BindView(R.id.tv_item_name_10)
    public TextView tvItemName10;
    @BindView(R.id.tv_man_hour_fee_10)
    public TextView tvManHourFee10;
    @BindView(R.id.tv_spare_part_name_10)
    public TextView tvSparePartName10;
    @BindView(R.id.tv_spare_part_number_10)
    public TextView tvSparePartNumber10;
    @BindView(R.id.tv_price_10)
    public TextView tvPrice10;

    @BindView(R.id.tv_item_name_11)
    public TextView tvItemName11;
    @BindView(R.id.tv_man_hour_fee_11)
    public TextView tvManHourFee11;
    @BindView(R.id.tv_spare_part_name_11)
    public TextView tvSparePartName11;
    @BindView(R.id.tv_spare_part_number_11)
    public TextView tvSparePartNumber11;
    @BindView(R.id.tv_price_11)
    public TextView tvPrice11;

    @BindView(R.id.tv_item_name_12)
    public TextView tvItemName12;
    @BindView(R.id.tv_man_hour_fee_12)
    public TextView tvManHourFee12;
    @BindView(R.id.tv_spare_part_name_12)
    public TextView tvSparePartName12;
    @BindView(R.id.tv_spare_part_number_12)
    public TextView tvSparePartNumber12;
    @BindView(R.id.tv_price_12)
    public TextView tvPrice12;

    @BindView(R.id.tv_item_name_13)
    public TextView tvItemName13;
    @BindView(R.id.tv_man_hour_fee_13)
    public TextView tvManHourFee13;
    @BindView(R.id.tv_spare_part_name_13)
    public TextView tvSparePartName13;
    @BindView(R.id.tv_spare_part_number_13)
    public TextView tvSparePartNumber13;
    @BindView(R.id.tv_price_13)
    public TextView tvPrice13;

    @BindView(R.id.tv_item_name_14)
    public TextView tvItemName14;
    @BindView(R.id.tv_man_hour_fee_14)
    public TextView tvManHourFee14;
    @BindView(R.id.tv_spare_part_name_14)
    public TextView tvSparePartName14;
    @BindView(R.id.tv_spare_part_number_14)
    public TextView tvSparePartNumber14;
    @BindView(R.id.tv_price_14)
    public TextView tvPrice14;

    @BindView(R.id.tv_item_name_15)
    public TextView tvItemName15;
    @BindView(R.id.tv_man_hour_fee_15)
    public TextView tvManHourFee15;
    @BindView(R.id.tv_spare_part_name_15)
    public TextView tvSparePartName15;
    @BindView(R.id.tv_spare_part_number_15)
    public TextView tvSparePartNumber15;
    @BindView(R.id.tv_price_15)
    public TextView tvPrice15;

    @BindView(R.id.tv_item_name_16)
    public TextView tvItemName16;
    @BindView(R.id.tv_man_hour_fee_16)
    public TextView tvManHourFee16;
    @BindView(R.id.tv_spare_part_name_16)
    public TextView tvSparePartName16;
    @BindView(R.id.tv_spare_part_number_16)
    public TextView tvSparePartNumber16;
    @BindView(R.id.tv_price_16)
    public TextView tvPrice16;

    @BindView(R.id.tv_receive_car_date)
    public TextView tvReceiveCarDate;
    @BindView(R.id.tv_plate_number)
    public TextView tvPlateNumber;
    @BindView(R.id.tv_car_brand)
    public TextView tvCarBrand;
    @BindView(R.id.tv_mileage)
    public TextView tvMileage;
    @BindView(R.id.tv_buy_car_date)
    public TextView tvBuyCarDate;
    @BindView(R.id.tv_man_hour_fee_total)
    public TextView tvManHourFeeTotal;
    @BindView(R.id.tv_material_fee_total)
    public TextView tvMaterialFeeTotal;
    @BindView(R.id.tv_operator)
    public TextView tvOperator;
    @BindView(R.id.btn_print_work_order)
    public Button btnPrintWorkOrder;
    @OnClick(R.id.btn_print_work_order)
    public void onPrintWorkOrder(View view) {
        try {
            InputStream inputStream = getAssets().open("workOrderTemplate.doc");
            FileUtils.writeFile(new File(templetPath), inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(!"".equals(workOrderContent)) {
            doScan();
        }
        else {

            }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_disp_work_order);

        ButterKnife.bind(this);
        initView();

    }

    private void initView() {
        AppManager.getAppManager().addActivity(this);
        userId = PreferencesUtils.getString(DispWorkOrderActivity.this, "userId");
        userName = PreferencesUtils.getString(DispWorkOrderActivity.this, "userName");
        carInfoId=getIntent().getStringExtra("carInfoId");
        tvUserName.setText(userName);
        getWorkOrder(carInfoId);

    }

    private void doScan(){
        int rgfhj=0;
        int clfhj=0;
        //获取模板文件
        File templateFile=new File(templetPath);
        //创建生成的文件
        File newFile=new File(newPath);
        Map<String, String> map = new HashMap<String, String>();
        String receivetCarDate=tvReceiveCarDate.getText().toString();
        map.put("$receiveCarDate$", receivetCarDate);
        String plateNumber=tvPlateNumber.getText().toString();
        map.put("$plateNumber$",plateNumber);
        String carBrand=tvCarBrand.getText().toString();
        map.put("$carBrand$", carBrand);
        String mileage=tvMileage.getText().toString();
        map.put("$mileage$", mileage);
        String buyCarTime=tvBuyCarDate.getText().toString();
        map.put("$buyCarTime$", buyCarTime);

        String itemName1=tvItemName1.getText().toString();
        map.put("$item1$", itemName1);
        String manHourFee1=tvManHourFee1.getText().toString();
        map.put("$rgf1$", manHourFee1);
        if(!"".equals(manHourFee1))
        {
            rgfhj+=Integer.parseInt(manHourFee1);
        }
        String sparePartName1=tvSparePartName1.getText().toString();
        map.put("$sparePartName1$", sparePartName1);
        String sparePartNumber1=tvSparePartNumber1.getText().toString();
        map.put("$num1$", sparePartNumber1);
        String price1=tvPrice1.getText().toString();
        map.put("$price1$", price1);
       if(!"".equals(price1))
       {
           clfhj+=Integer.parseInt(price1);
       }

        String itemName2=tvItemName2.getText().toString();
        map.put("$item2$", itemName2);
        String manHourFee2=tvManHourFee2.getText().toString();
        map.put("$rgf2$", manHourFee2);
        if(!"".equals(manHourFee2))
        {
            rgfhj+=Integer.parseInt(manHourFee2);
        }
        String sparePartName2=tvSparePartName2.getText().toString();
        map.put("$sparePartName2$", sparePartName2);
        String sparePartNumber2=tvSparePartNumber2.getText().toString();
        map.put("$num2$", sparePartNumber2);
        String price2=tvPrice2.getText().toString();
        map.put("$price2$", price2);
        if(!"".equals(price2))
        {
            clfhj+=Integer.parseInt(price2);
        }

        String itemName3=tvItemName3.getText().toString();
        map.put("$item3$", itemName3);
        String manHourFee3=tvManHourFee3.getText().toString();
        map.put("$rgf3$", manHourFee3);
        if(!"".equals(manHourFee3))
        {
            rgfhj+=Integer.parseInt(manHourFee3);
        }
        String sparePartName3=tvSparePartName3.getText().toString();
        map.put("$sparePartName3$", sparePartName3);
        String sparePartNumber3=tvSparePartNumber3.getText().toString();
        map.put("$num3$", sparePartNumber3);
        String price3=tvPrice3.getText().toString();
        map.put("$price3$", price3);
        if(!"".equals(price3))
        {
            clfhj+=Integer.parseInt(price3);
        }


        String itemName4=tvItemName4.getText().toString();
        map.put("$item4$", itemName4);
        String manHourFee4=tvManHourFee4.getText().toString();
        map.put("$rgf4$", manHourFee4);
        if(!"".equals(manHourFee4))
        {
            rgfhj+=Integer.parseInt(manHourFee4);
        }
        String sparePartName4=tvSparePartName4.getText().toString();
        map.put("$sparePartName4$", sparePartName4);
        String sparePartNumber4=tvSparePartNumber4.getText().toString();
        map.put("$num4$", sparePartNumber4);
        String price4=tvPrice4.getText().toString();
        map.put("$price4$", price4);
        if(!"".equals(price4))
        {
            clfhj+=Integer.parseInt(price4);
        }

        String itemName5=tvItemName5.getText().toString();
        map.put("$item5$", itemName5);
        String manHourFee5=tvManHourFee5.getText().toString();
        map.put("$rgf5$", manHourFee5);
        if(!"".equals(manHourFee5))
        {
            rgfhj+=Integer.parseInt(manHourFee5);
        }
        String sparePartName5=tvSparePartName5.getText().toString();
        map.put("$sparePartName5$", sparePartName5);
        String sparePartNumber5=tvSparePartNumber5.getText().toString();
        map.put("$num5$", sparePartNumber5);
        String price5=tvPrice5.getText().toString();
        map.put("$price5$", price5);
        if(!"".equals(price5))
        {
            clfhj+=Integer.parseInt(price5);
        }


        String itemName6=tvItemName6.getText().toString();
        map.put("$item6$", itemName6);
        String manHourFee6=tvManHourFee6.getText().toString();
        map.put("$rgf6$", manHourFee6);
        if(!"".equals(manHourFee6))
        {
            rgfhj+=Integer.parseInt(manHourFee6);
        }
        String sparePartName6=tvSparePartName6.getText().toString();
        map.put("$sparePartName6$", sparePartName6);
        String sparePartNumber6=tvSparePartNumber6.getText().toString();
        map.put("$num6$", sparePartNumber6);
        String price6=tvPrice6.getText().toString();
        map.put("$price6$", price6);
        if(!"".equals(price6))
        {
            clfhj+=Integer.parseInt(price6);
        }

        String itemName7=tvItemName7.getText().toString();
        map.put("$item7$", itemName7);
        String manHourFee7=tvManHourFee7.getText().toString();
        map.put("$rgf7$", manHourFee7);
        if(!"".equals(manHourFee7))
        {
            rgfhj+=Integer.parseInt(manHourFee7);
        }
        String sparePartName7=tvSparePartName7.getText().toString();
        map.put("$sparePartName7$", sparePartName7);
        String sparePartNumber7=tvSparePartNumber7.getText().toString();
        map.put("$num7$", sparePartNumber7);
        String price7=tvPrice7.getText().toString();
        map.put("$price7$", price7);
        if(!"".equals(price7))
        {
            clfhj+=Integer.parseInt(price7);
        }

        String itemName8=tvItemName8.getText().toString();
        map.put("$item8$", itemName8);
        String manHourFee8=tvManHourFee8.getText().toString();
        map.put("$rgf8$", manHourFee8);
        if(!"".equals(manHourFee8))
        {
            rgfhj+=Integer.parseInt(manHourFee8);
        }
        String sparePartName8=tvSparePartName8.getText().toString();
        map.put("$sparePartName8$", sparePartName8);
        String sparePartNumber8=tvSparePartNumber8.getText().toString();
        map.put("$num8$", sparePartNumber8);
        String price8=tvPrice8.getText().toString();
        map.put("$price8$", price8);
        if(!"".equals(price8))
        {
            clfhj+=Integer.parseInt(price8);
        }

        String itemName9=tvItemName9.getText().toString();
        map.put("$item9$", itemName9);
        String manHourFee9=tvManHourFee9.getText().toString();
        map.put("$rgf9$", manHourFee9);
        if(!"".equals(manHourFee9))
        {
            rgfhj+=Integer.parseInt(manHourFee9);
        }
        String sparePartName9=tvSparePartName9.getText().toString();
        map.put("$sparePartName9$", sparePartName9);
        String sparePartNumber9=tvSparePartNumber9.getText().toString();
        map.put("$num9$", sparePartNumber9);
        String price9=tvPrice9.getText().toString();
        map.put("$price9$", price9);
        if(!"".equals(price9))
        {
            clfhj+=Integer.parseInt(price9);
        }

        String itemName10=tvItemName10.getText().toString();
        map.put("$item10$", itemName10);
        String manHourFee10=tvManHourFee10.getText().toString();
        map.put("$rgf10$", manHourFee10);
        if(!"".equals(manHourFee10))
        {
            rgfhj+=Integer.parseInt(manHourFee10);
        }
        String sparePartName10=tvSparePartName10.getText().toString();
        map.put("$sparePartName10$", sparePartName10);
        String sparePartNumber10=tvSparePartNumber10.getText().toString();
        map.put("$num10$", sparePartNumber10);
        String price10=tvPrice10.getText().toString();
        map.put("$price10$", price10);
        if(!"".equals(price10))
        {
            clfhj+=Integer.parseInt(price10);
        }

        String itemName11=tvItemName11.getText().toString();
        map.put("$item11$", itemName11);
        String manHourFee11=tvManHourFee11.getText().toString();
        map.put("$rgf11$", manHourFee11);
        if(!"".equals(manHourFee11))
        {
            rgfhj+=Integer.parseInt(manHourFee11);
        }
        String sparePartName11=tvSparePartName11.getText().toString();
        map.put("$sparePartName11$", sparePartName11);
        String sparePartNumber11=tvSparePartNumber11.getText().toString();
        map.put("$num11$", sparePartNumber11);
        String price11=tvPrice11.getText().toString();
        map.put("$price11$", price11);
        if(!"".equals(price11))
        {
            clfhj+=Integer.parseInt(price11);
        }

        String itemName12=tvItemName12.getText().toString();
        map.put("$item12$", itemName12);
        String manHourFee12=tvManHourFee12.getText().toString();
        map.put("$rgf12$", manHourFee12);
        if(!"".equals(manHourFee12))
        {
            rgfhj+=Integer.parseInt(manHourFee12);
        }
        String sparePartName12=tvSparePartName12.getText().toString();
        map.put("$sparePartName12$", sparePartName12);
        String sparePartNumber12=tvSparePartNumber12.getText().toString();
        map.put("$num12$", sparePartNumber12);
        String price12=tvPrice12.getText().toString();
        map.put("$price12$", price12);
        if(!"".equals(price12))
        {
            clfhj+=Integer.parseInt(price12);
        }
        String itemName13=tvItemName13.getText().toString();
        map.put("$item13$", itemName13);
        String manHourFee13=tvManHourFee13.getText().toString();
        map.put("$rgf13$", manHourFee13);
        if(!"".equals(manHourFee13))
        {
            rgfhj+=Integer.parseInt(manHourFee13);
        }
        String sparePartName13=tvSparePartName13.getText().toString();
        map.put("$sparePartName13$", sparePartName13);
        String sparePartNumber13=tvSparePartNumber13.getText().toString();
        map.put("$num13$", sparePartNumber13);
        String price13=tvPrice13.getText().toString();
        map.put("$price13$", price13);
        if(!"".equals(price13))
        {
            clfhj+=Integer.parseInt(price13);
        }

        String itemName14=tvItemName14.getText().toString();
        map.put("$item14$", itemName14);
        String manHourFee14=tvManHourFee14.getText().toString();
        map.put("$rgf14$", manHourFee14);
        if(!"".equals(manHourFee14))
        {
            rgfhj+=Integer.parseInt(manHourFee14);
        }
        String sparePartName14=tvSparePartName14.getText().toString();
        map.put("$sparePartName14$", sparePartName14);
        String sparePartNumber14=tvSparePartNumber14.getText().toString();
        map.put("$num14$", sparePartNumber14);
        String price14=tvPrice14.getText().toString();
        map.put("$price14$", price14);
        if(!"".equals(price14))
        {
            clfhj+=Integer.parseInt(price14);
        }

        String itemName15=tvItemName15.getText().toString();
        map.put("$item15$", itemName15);
        String manHourFee15=tvManHourFee15.getText().toString();
        map.put("$rgf15$", manHourFee15);
        if(!"".equals(manHourFee15))
        {
            rgfhj+=Integer.parseInt(manHourFee15);
        }
        String sparePartName15=tvSparePartName15.getText().toString();
        map.put("$sparePartName15$", sparePartName15);
        String sparePartNumber15=tvSparePartNumber15.getText().toString();
        map.put("$num15$", sparePartNumber15);
        String price15=tvPrice15.getText().toString();
        map.put("$price15$", price15);
        if(!"".equals(price15))
        {
            clfhj+=Integer.parseInt(price15);
        }

        String itemName16=tvItemName16.getText().toString();
        map.put("$item16$", itemName16);
        String manHourFee16=tvManHourFee16.getText().toString();
        map.put("$rgf16$", manHourFee16);
        if(!"".equals(manHourFee16))
        {
            rgfhj+=Integer.parseInt(manHourFee16);
        }
        String sparePartName16=tvSparePartName16.getText().toString();
        map.put("$sparePartName16$", sparePartName16);
        String sparePartNumber16=tvSparePartNumber16.getText().toString();
        map.put("$num16$", sparePartNumber16);
        String price16=tvPrice16.getText().toString();
        map.put("$price16$", price16);
        if(!"".equals(price16))
        {
            clfhj+=Integer.parseInt(price16);
        }

        map.put("$gsfhj$", rgfhj+"");
        map.put("$clfhj$", clfhj+"");
        map.put("$operator$", userName);

        writeDoc(templateFile,newFile,map);
        //查看
        doOpenWord();
    }
    private static void grantUriPermission (Context context, Uri fileUri, Intent intent) {
        List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        for (ResolveInfo resolveInfo : resInfoList) {
            String packageName = resolveInfo.activityInfo.packageName;
            context.grantUriPermission(packageName, fileUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
    }

    /**
     * 调用手机中安装的可打开word的软件
     */
    private void doOpenWord(){
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        String fileMimeType = "application/msword";
       // intent.setDataAndType(Uri.fromFile(new File(newPath)), fileMimeType);
        Uri fileUri = FileProvider.getUriForFile(DispWorkOrderActivity.this, DispWorkOrderActivity.this.getPackageName()+".provider", new File(newPath));
        //android 7.0以上
        intent.setDataAndType(fileUri, fileMimeType);
        grantUriPermission(DispWorkOrderActivity.this, fileUri, intent);

        try{
            DispWorkOrderActivity.this.startActivity(intent);
        } catch(ActivityNotFoundException e) {
            //检测到系统尚未安装OliveOffice的apk程序
            Toast.makeText(DispWorkOrderActivity.this, "未找到软件", Toast.LENGTH_LONG).show();

        }
        catch (Exception e)
        {
            String err=e.getMessage();
        }
    }
    /**
     * templateFile 模板文件
     * newFile 生成文件
     * map 要填充的数据
     * */
    public void writeDoc(File templateFile ,File newFile ,Map<String, String> map)
    {
        try
        {
            FileInputStream in = new FileInputStream(templateFile);
            HWPFDocument hdt = new HWPFDocument(in);
            // Fields fields = hdt.getFields();
            // 读取word文本内容
            Range range = hdt.getRange();
            // System.out.println(range.text());

            // 替换文本内容
            for(Map.Entry<String, String> entry : map.entrySet())
            {
                range.replaceText(entry.getKey(), entry.getValue());
            }
            if(newFile.exists())
            {
                newFile.delete();
            }
            ByteArrayOutputStream ostream = new ByteArrayOutputStream();
            FileOutputStream out = new FileOutputStream(newFile, true);
            hdt.write(ostream);
            // 输出字节流
            out.write(ostream.toByteArray());
            out.close();
            ostream.close();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    private Handler myHandler = new Handler() {
        String key="";
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            String key="";
            switch (msg.what) {

                case MSG_GET_WORK_ORDER:
                    key= msg.getData().keySet().iterator().next();
                    if (BUNDLE_MSG_GET_WORK_ORDER_FAIL.equals(key)) {
                        if (progressDialog != null) {
                            progressDialog.dismiss();
                            progressDialog = null;
                        }
                        Toast.makeText(DispWorkOrderActivity.this,msg.getData().getString(BUNDLE_MSG_GET_WORK_ORDER_FAIL), Toast.LENGTH_SHORT).show();
                    }
                    else if (BUNDLE_LOCAL_ERROR.equals(key)) {
                        if (progressDialog != null) {
                            progressDialog.dismiss();
                            progressDialog = null;
                        }
                        Toast.makeText(DispWorkOrderActivity.this,"服务器连接失败！", Toast.LENGTH_SHORT).show();

                    }

                    else if (BUNDLE_MSG_GET_WORK_ORDER_SUCCESS.equals(key)) {
                        if (progressDialog != null) {
                            progressDialog.dismiss();
                            progressDialog = null;
                        }
                         workOrderContent=msg.getData().getString(BUNDLE_MSG_GET_WORK_ORDER_SUCCESS);
                        if(!"".equals(workOrderContent))
                        {
                            try{

                                com.alibaba.fastjson.JSONObject obj = com.alibaba.fastjson.JSONObject.parseObject(workOrderContent);
                                String receiveCarDate=obj.getString("receiveCarDate");
                                tvReceiveCarDate.setText(receiveCarDate);
                                String plateNumber=obj.getString("plateNumber");
                                tvPlateNumber.setText(plateNumber);
                                String carBrand=obj.getString("carBrand");
                                tvCarBrand.setText(carBrand);
                                String  buyCarDate=obj.getString("buyCarDate");
                                tvBuyCarDate.setText(buyCarDate);
                                String  mileage=obj.getString("mileage");
                                tvMileage.setText(mileage);
                                String manHourFeeTotal=obj.getString("manHourFeeTotal");
                                tvManHourFeeTotal.setText("工时费合计："+manHourFeeTotal);
                                String materialFeeTotal=obj.getString("materialFeeTotal");
                                tvMaterialFeeTotal.setText("材料费合计："+materialFeeTotal);
                                String operator=obj.getString("operator");
                                tvOperator.setText("操作员："+operator);

                                JSONArray contentArray= obj.getJSONArray("workOrderItem");
                                String itemId="";
                                String itemName="";
                                String manHourFee="";
                                String sparePartName="";
                                String sparePartNumber="";
                                String price="";
                                for(int i=0;i<contentArray.size();i++) {
                                    // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                                    com.alibaba.fastjson.JSONObject job = contentArray.getJSONObject(i);
                                    itemId=job.getString("itemId");
                                    itemName=job.getString("itemName");
                                    manHourFee=job.getString("manHourFee");
                                    sparePartName=job.getString("sparePartName");
                                    sparePartNumber=job.getString("sparePartNumber");
                                    price=job.getString("price");
                                    switch (i) {
                                        case  0:
                                            tvItemName1.setText(itemName);
                                            tvManHourFee1.setText(manHourFee);
                                            tvSparePartName1.setText(sparePartName);
                                            tvSparePartNumber1.setText(sparePartNumber);
                                            tvPrice1.setText(price);
                                            break;
                                        case  1:
                                            tvItemName2.setText(itemName);
                                            tvManHourFee2.setText(manHourFee);
                                            tvSparePartName2.setText(sparePartName);
                                            tvSparePartNumber2.setText(sparePartNumber);
                                            tvPrice2.setText(price);
                                            break;
                                        case  2:
                                            tvItemName3.setText(itemName);
                                            tvManHourFee3.setText(manHourFee);
                                            tvSparePartName3.setText(sparePartName);
                                            tvSparePartNumber3.setText(sparePartNumber);
                                            tvPrice3.setText(price);
                                            break;
                                        case  3:
                                            tvItemName4.setText(itemName);
                                            tvManHourFee4.setText(manHourFee);
                                            tvSparePartName4.setText(sparePartName);
                                            tvSparePartNumber4.setText(sparePartNumber);
                                            tvPrice4.setText(price);
                                            break;
                                        case  4:
                                            tvItemName5.setText(itemName);
                                            tvManHourFee5.setText(manHourFee);
                                            tvSparePartName5.setText(sparePartName);
                                            tvSparePartNumber5.setText(sparePartNumber);
                                            tvPrice5.setText(price);
                                            break;
                                        case  5:
                                            tvItemName6.setText(itemName);
                                            tvManHourFee6.setText(manHourFee);
                                            tvSparePartName6.setText(sparePartName);
                                            tvSparePartNumber6.setText(sparePartNumber);
                                            tvPrice6.setText(price);
                                            break;
                                        case  6:
                                            tvItemName7.setText(itemName);
                                            tvManHourFee7.setText(manHourFee);
                                            tvSparePartName7.setText(sparePartName);
                                            tvSparePartNumber7.setText(sparePartNumber);
                                            tvPrice7.setText(price);
                                            break;
                                        case  7:
                                            tvItemName8.setText(itemName);
                                            tvManHourFee8.setText(manHourFee);
                                            tvSparePartName8.setText(sparePartName);
                                            tvSparePartNumber8.setText(sparePartNumber);
                                            tvPrice8.setText(price);
                                            break;
                                        case  8:
                                            tvItemName9.setText(itemName);
                                            tvManHourFee9.setText(manHourFee);
                                            tvSparePartName9.setText(sparePartName);
                                            tvSparePartNumber9.setText(sparePartNumber);
                                            tvPrice9.setText(price);
                                            break;
                                        case  9:
                                            tvItemName10.setText(itemName);
                                            tvManHourFee10.setText(manHourFee);
                                            tvSparePartName10.setText(sparePartName);
                                            tvSparePartNumber10.setText(sparePartNumber);
                                            tvPrice10.setText(price);
                                            break;
                                        case  10:
                                            tvItemName11.setText(itemName);
                                            tvManHourFee11.setText(manHourFee);
                                            tvSparePartName11.setText(sparePartName);
                                            tvSparePartNumber11.setText(sparePartNumber);
                                            tvPrice11.setText(price);
                                            break;
                                        case  11:
                                            tvItemName12.setText(itemName);
                                            tvManHourFee12.setText(manHourFee);
                                            tvSparePartName12.setText(sparePartName);
                                            tvSparePartNumber12.setText(sparePartNumber);
                                            tvPrice12.setText(price);
                                            break;
                                        case  12:
                                            tvItemName13.setText(itemName);
                                            tvManHourFee13.setText(manHourFee);
                                            tvSparePartName13.setText(sparePartName);
                                            tvSparePartNumber13.setText(sparePartNumber);
                                            tvPrice13.setText(price);
                                            break;
                                        case  13:
                                            tvItemName14.setText(itemName);
                                            tvManHourFee14.setText(manHourFee);
                                            tvSparePartName14.setText(sparePartName);
                                            tvSparePartNumber14.setText(sparePartNumber);
                                            tvPrice14.setText(price);
                                            break;
                                        case  14:
                                            tvItemName15.setText(itemName);
                                            tvManHourFee15.setText(manHourFee);
                                            tvSparePartName15.setText(sparePartName);
                                            tvSparePartNumber15.setText(sparePartNumber);
                                            tvPrice15.setText(price);
                                            break;
                                        case  15:
                                            tvItemName16.setText(itemName);
                                            tvManHourFee16.setText(manHourFee);
                                            tvSparePartName16.setText(sparePartName);
                                            tvSparePartNumber16.setText(sparePartNumber);
                                            tvPrice16.setText(price);
                                            break;


                                    }
                                }

                            }
                            catch (Exception e)
                            {
                                String error=e.getMessage();
                            }
                        }

                    }
                    break;

                default:
                    break;
            }
            removeMessages(msg.what);
        }
    };
    protected void getWorkOrder(String carInfoId)
    {

        if(Utils.isNetworkAvailable(DispWorkOrderActivity.this)==false)
        {
            Toast.makeText(DispWorkOrderActivity.this,"网络尚未连接，请连接网络！",Toast.LENGTH_SHORT).show();
            return;
        }

        final String []key={"carInfoId"};
        final String []value={carInfoId};
        progressDialog = ProgressDialog.show(DispWorkOrderActivity.this,
                getResources().getString(R.string.notice), "正在加载工单...");
        new Thread(new Runnable() {
            @Override
            public void run() {
                Bundle bundle = new Bundle();
                try {
                    String rs = WSClient.soapGetInfo("getWorkOrder",key,value);
                    JSONTokener jsonTokener = new JSONTokener(rs);
                    JSONObject jsonObject = (JSONObject) jsonTokener
                            .nextValue();
                    String status = jsonObject.getString("status");
                    if("1".equals(status))						{

                        bundle.putString(BUNDLE_MSG_GET_WORK_ORDER_SUCCESS,rs);
                    }
                    else
                    {
                        bundle.putString(BUNDLE_MSG_GET_WORK_ORDER_FAIL, "工单加载失败，请重试！");
                    }
                }
                catch (Exception e) {

                    bundle.putString(BUNDLE_LOCAL_ERROR, "访问服务器失败！");
                }
                Message msg = myHandler.obtainMessage();
                msg.what = MSG_GET_WORK_ORDER;
                msg.setData(bundle);
                myHandler.sendMessage(msg);
            }
        }).start();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getAppManager().finishActivity(this);
    }
}
